<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="$t('msg.search.'+item.name)">
        <el-select  v-if="item.itemType === 'DICT'" v-model="item.value" :multiple = "item.mutSelect ==='checkBox'">
          <el-option  v-for="(itemChild, itemIndex) in item.dict" :key="itemIndex"  :label="itemChild.value" :value="itemChild.key" />
        </el-select>
        <el-input v-else-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.dateValue"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{$t('msg.common.search')}}</el-button>
        <el-button @click="onReset">{{$t('msg.common.reset')}}</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column type="expand">
        <template #default="scope">
          <el-form :inline="true" v-model="scope.row">
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="10">
                <el-form-item label="岗位名称:">{{scope.row.name}}</el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="科室:">{{scope.row.department}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="20">
                <el-form-item label="岗位描述:">{{scope.row.described}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="10">
                <el-form-item label="所需专业:">{{scope.row.profession}}</el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="岗位类别:">{{scope.row.jobType}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="10">
                <el-form-item label="岗位级别:">{{scope.row.jobLevel}}</el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="人员类别:">{{scope.row.personType}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="10">
                <el-form-item label="招聘人数:">{{scope.row.nums}}</el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="最高年龄:">{{scope.row.maxAge}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="10">
                <el-form-item label="最低学历:">{{scope.row.lowBachelor}}</el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="最低学位:">{{scope.row.lowDegree}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="4">
              </el-col>
              <el-col :span="20">
                <el-form-item label="其他条件:">{{scope.row.otherCondition}}</el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="index" width="80" :label="$t('msg.common.serial')"/>
      <el-table-column prop="createTime" :label="$t('msg.common.createTime')" width="160" />
      <el-table-column prop="code" label="编码" width="120" />
      <el-table-column prop="name" label="岗位" width="120" />
      <el-table-column prop="publishDate" label="发布日期" width="160" />
      <el-table-column prop="closed" label="是否停招" width="100" />
      <el-table-column prop="endDate" label="截至日期" width="160" />
      <el-table-column fixed="right" :label="$t('msg.common.operation')" width="140">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
           <el-button text type="success" size="small" @click="handleApply(scope.row.id)">申请</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { ElMessage } from 'element-plus'
import { list, deleteById, downLoadFile, add, modify } from '@/api/common/common'
import { apply } from '@/api/applicant/job'
import { useI18n } from 'vue-i18n'
import { dateToString, dateToNext, dateTimeToString, dToS } from '@/utils/date'
const tableDatas = ref({})

const tableData = ref([])

const currentJob = ref({})

const dialogVisible = ref(false)

const i18n = useI18n()

const searchPage = ref({
  items: [
    {
      name: 'name',
      itemType: 'STRING',
      sign: 'like',
      value: '',
      dateValue: ''
    },
    {
      name: 'described',
      itemType: 'STRING',
      sign: 'like',
      value: '',
      dateValue: ''
    },
    {
      name: 'createTime1',
      itemType: 'DATE',
      sign: '>=',
      value: dateToString(-30),
      dateValue: dateToNext(-30)
    },
    {
      name: 'createTime2',
      itemType: 'DATE',
      sign: '<=',
      value: dateToString(0),
      dateValue: dateToNext(0)
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const searchFunction = async () => {
  list(searchPage.value, 'job').then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success(i18n.t('msg.common.success'))
  }).catch(err => {
    ElMessage.success(i18n.t('msg.common.error') || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'name',
        itemType: 'STRING',
        sign: 'like',
        value: '',
        dateValue: ''
      },
      {
        name: 'described',
        itemType: 'STRING',
        sign: 'like',
        value: '',
        dateValue: ''
      },
      {
        name: 'createTime1',
        itemType: 'DATE',
        sign: '>=',
        value: dateToString(-30),
        dateValue: dateToNext(-30)
      },
      {
        name: 'createTime2',
        itemType: 'DATE',
        sign: '<=',
        value: dateToString(0),
        dateValue: dateToNext(0)
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

// 申请
const handleApply = id => {
  apply(id).then(res => {
    ElMessage.success('申请成功，请到申请列表查看')
  })
}

</script>

<style lang="scss" scoped>
.search-container {
  width: 100%;
  height: 100px;
}
</style>
